package com.ple.app.suggest;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.ple.app.importdata.Inputcultivate_Activity;
import com.ple.app.importdata.MyopenHelper;
import com.ple.app.jzsf.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class getcultivateActivity extends Activity {
    private  final String[] culitivate_conlm={"ID","crop","seed","date","manure_number"};
    private ListView listView;
    private String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_getcultivate);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        MyopenHelper myopenhelper=new MyopenHelper(getcultivateActivity.this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("cultivate",culitivate_conlm,null,null,null,null,null,null);
        if(curor.moveToLast()){
            for(curor.moveToLast();!curor.isFirst();curor.moveToPrevious()){
                Map<String, Object> item = new HashMap<String, Object>();
                String id=curor.getString(0);
                String crop=curor.getString(1);
                String seed=curor.getString(2);
                String data=curor.getString(3);
                String manure=curor.getString(4);
                item.put("ID", id);
                item.put("crop", crop);
                item.put("seed", seed);
                item.put("date",data);
                item.put("manure_number", manure);
                list.add(item);}}
        else{return;}
        curor.close();
        db.close();
        SimpleAdapter adapter = new SimpleAdapter(getcultivateActivity.this, list, R.layout.cultitave_item,
                new String[]{"ID", "crop", "seed", "date","manure_number"},
                new int[]{R.id.cultitaID, R.id.crop_Name, R.id.seed_type, R.id.cultita_time,R.id.cultita_manure});
        listView = (ListView) findViewById(R.id.cultivate_info);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String,String> getmap= (HashMap<String, String>) listView.getItemAtPosition(position);
                name=getmap.get("crop");

                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putString("name",name);
                intent.putExtras(bundle);
                setResult(0,intent);
                finish();
            }
        });

    }

}
